/*
 * Copyright (c) 2021
 * User:mallplus多商户商城
 * File:SmsHomeRecommendProductServiceImpl.java
 * Date:2021/01/12 19:29:12
 */

package com.qyine.rlmall.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qyine.rlmall.sms.entity.SmsHomeRecommendProduct;
import com.qyine.rlmall.sms.mapper.SmsHomeRecommendProductMapper;
import com.qyine.rlmall.sms.service.ISmsHomeRecommendProductService;
import com.qyine.rlmall.enums.StatusEnum;
import com.qyine.rlmall.util.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 人气推荐商品表 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-19
 */
@Service
public class SmsHomeRecommendProductServiceImpl extends ServiceImpl<SmsHomeRecommendProductMapper, SmsHomeRecommendProduct> implements ISmsHomeRecommendProductService {
    @Resource
    private SmsHomeRecommendProductMapper recommendProductMapper;

    @Override
    public int updateSort(Long id, Integer sort) {
        SmsHomeRecommendProduct recommendProduct = new SmsHomeRecommendProduct();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        return recommendProductMapper.updateById(recommendProduct);
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        SmsHomeRecommendProduct record = new SmsHomeRecommendProduct();
        record.setRecommendStatus(recommendStatus);
        return recommendProductMapper.update(record, new QueryWrapper<SmsHomeRecommendProduct>().in("id", ids));
    }

    @Override
    public int create(List<SmsHomeRecommendProduct> homeRecommendProductList) {
        for (SmsHomeRecommendProduct recommendProduct : homeRecommendProductList) {
            SmsHomeRecommendProduct newB = recommendProductMapper.selectOne(new QueryWrapper<>(new SmsHomeRecommendProduct()).eq("product_id",recommendProduct.getProductId()));
            if (newB==null) {
                recommendProduct.setStoreId(UserUtils.getCurrentMember().getStoreId());
                recommendProduct.setRecommendStatus(StatusEnum.YesNoType.YES.code());
                recommendProduct.setSort(0);
                recommendProductMapper.insert(recommendProduct);
            }
        }
        return homeRecommendProductList.size();
    }
}
